package com.titan.demo.camel.utils;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.ProducerTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.titan.core.utils.common.domain.KeyValue;
import com.titan.core.utils.debug.LogUtils;
import com.titan.core.utils.spring.Config;
import com.titan.core.utils.spring.SpringUtils;

public class Context {
	private final static Logger log = LoggerFactory.getLogger(Context.class);
	public final static String LINE = System.getProperty("line.separator");
	
	public static void initSpring(String...args) {
		SpringUtils.initApplicationContext(new ClassPathXmlApplicationContext(args));
		
		// 打印启动日志
		LogUtils.info(log, "Spring[{}] initialized！", args);
		LogUtils.info(log, "Version：{}", Config.getConfig("LastUpdateDate"));
	}
	
	
	public static Exchange send(String endpointName, KeyValue<String,Object>[] headers, Object body) {
		return send("camel-1", endpointName, headers, body);
	}
	public static Exchange send(String camelid, String endpointName, KeyValue<String,Object>[] headers, Object body) {
		CamelContext camelcontext = (CamelContext)SpringUtils.getBean(camelid);
		camelcontext.setAllowUseOriginalMessage(false);
		Endpoint endpoint = camelcontext.getEndpoint(endpointName);
		Exchange exchange = endpoint.createExchange();
		Message in = exchange.getIn();
		if( headers!=null ) {
			int len = headers.length;
			for (int i = 0; i < len; i++) {
				KeyValue<String,Object> value = headers[i];
//				System.out.println(value.getKey()+" == "+value.getValue());
				in.setHeader(value.getKey(), value.getValue());
			}
		}
		in.setBody(body);
		
		// now we send the exchange to the endpoint, and receives the response from Camel
		ProducerTemplate template = camelcontext.createProducerTemplate();
		Exchange out = template.send(endpoint, exchange);
		return out;
	}	
	public static Exchange send(String camelid, String endpointName, Object body, Object...headers) {
		CamelContext camelcontext = (CamelContext)SpringUtils.getBean(camelid);
		camelcontext.setAllowUseOriginalMessage(false);
		Endpoint endpoint = camelcontext.getEndpoint(endpointName);
		Exchange exchange = endpoint.createExchange();
		Message in = exchange.getIn();
		if( headers!=null ) {
			int len = headers.length;
			for (int i = 0; (i+1) < len; i=i+2) {
//				System.out.println(headers[i]+" == "+headers[i+1]);
				in.setHeader((String)headers[i], headers[i+1]);
			}
		}
		in.setBody(body);
		
		// now we send the exchange to the endpoint, and receives the response from Camel
		ProducerTemplate template = camelcontext.createProducerTemplate();
		Exchange out = template.send(endpoint, exchange);
		return out;		
	}
	
	
	private static ExecutorService pool = null;
	public static void initPool(int nThreads) {
		// 初始化线程池
		if(nThreads<=0) {
			LogUtils.info(log, "Prohibit initialize the thread pool！");
		} else {
			pool = Executors.newFixedThreadPool(nThreads);
			LogUtils.info(log, "Threads[number={}] initialized！", nThreads);
		}		
	}
	public static ExecutorService getPool() {
		return pool;
	}
	public static void execute(Runnable runnable) {
		pool.execute(runnable);
	}

}

